<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  
  <title>Java笔记—对象、类 | MelodyHub</title>
  
  

  

  <meta name="HandheldFriendly" content="True" />
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <!-- meta -->
  
  
  <meta name='theme-color' content='#FFFFFF'>
  <meta name='msapplication-TileColor' content='#1BC3FB'>
  <meta name='msapplication-config' content='https://cdn.jsdelivr.net/gh/xaoxuu/assets@master/favicon/favicons/browserconfig.xml'>
  

  <!-- link -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-waves@0.7.6/dist/waves.min.css">
  
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.10.1/css/all.min.css">
  
  
  <link rel='shortcut icon' type='image/x-icon' href='https://cdn.jsdelivr.net/gh/xaoxuu/assets@master/favicon/favicon.ico'>
  <link rel='icon' type='image/x-icon' sizes='32x32' href='https://cdn.jsdelivr.net/gh/xaoxuu/assets@master/favicon/favicons/favicon-32x32.png'>
  <link rel='apple-touch-icon' type='image/png' sizes='180x180' href='https://cdn.jsdelivr.net/gh/xaoxuu/assets@master/favicon/favicons/apple-touch-icon.png'>
  <link rel='mask-icon' color='#1BC3FB' href='https://cdn.jsdelivr.net/gh/xaoxuu/assets@master/favicon/favicons/safari-pinned-tab.svg'>
  <link rel='manifest' href='https://cdn.jsdelivr.net/gh/xaoxuu/assets@master/favicon/favicons/site.webmanifest'>
  

  

  
    
<link rel="stylesheet" href="/blog/style.css">

  

  <script>
    function setLoadingBarProgress(num) {
      document.getElementById('loading-bar').style.width=num+"%";
    }
  </script>

  
  
<meta name="generator" content="Hexo 4.2.0"></head>

<body>
  
  
  <div class="cover-wrapper">
    <cover class='cover post half'>
      
        
  <h1 class='title'>Demo</h1>


  <div class="m_search">
    <form name="searchform" class="form u-search-form">
      <input type="text" class="input u-search-input" placeholder="" />
      <i class="icon fas fa-search fa-fw"></i>
    </form>
  </div>

<div class='menu navgation'>
  <ul class='h-list'>
    
      
        <li>
          <a class="nav home" href="/blog/"
            
            
            id="blog">
            <i class='fas fa-rss fa-fw'></i>&nbsp;博文
          </a>
        </li>
      
        <li>
          <a class="nav home" href="/blog/projects/"
            
            
            id="blogprojects">
            <i class='fas fa-code-branch fa-fw'></i>&nbsp;项目
          </a>
        </li>
      
        <li>
          <a class="nav home" href="/blog/friends/"
            
              rel="nofollow"
            
            
            id="blogfriends">
            <i class='fas fa-link fa-fw'></i>&nbsp;友链
          </a>
        </li>
      
        <li>
          <a class="nav home" href="/blog/about/"
            
              rel="nofollow"
            
            
            id="blogabout">
            <i class='fas fa-info-circle fa-fw'></i>&nbsp;关于
          </a>
        </li>
      
    
  </ul>
</div>

      
    </cover>
    <header class="l_header pure">
  <div id="loading-bar-wrapper">
    <div id="loading-bar" class="pure"></div>
  </div>

	<div class='wrapper'>
		<div class="nav-main container container--flex">
      <a class="logo flat-box" href='/blog/' >
        
          MelodyHub
        
      </a>
			<div class='menu navgation'>
				<ul class='h-list'>
          
  					
  						<li>
								<a class="nav flat-box" href="/blog/"
                  
                  
                  id="blog">
									<i class='fas fa-grin fa-fw'></i>&nbsp;近期博文
								</a>
							</li>
      			
  						<li>
								<a class="nav flat-box" href="/blog/blog/categories/"
                  
                    rel="nofollow"
                  
                  
                  id="blogblogcategories">
									<i class='fas fa-folder-open fa-fw'></i>&nbsp;分类
								</a>
							</li>
      			
  						<li>
								<a class="nav flat-box" href="/blog/blog/tags/"
                  
                    rel="nofollow"
                  
                  
                  id="blogblogtags">
									<i class='fas fa-hashtag fa-fw'></i>&nbsp;标签
								</a>
							</li>
      			
  						<li>
								<a class="nav flat-box" href="/blog/blog/archives/"
                  
                    rel="nofollow"
                  
                  
                  id="blogblogarchives">
									<i class='fas fa-archive fa-fw'></i>&nbsp;归档
								</a>
							</li>
      			
      		
				</ul>
			</div>

			
				<div class="m_search">
					<form name="searchform" class="form u-search-form">
						<input type="text" class="input u-search-input" placeholder="搜索" />
						<i class="icon fas fa-search fa-fw"></i>
					</form>
				</div>
			
			<ul class='switcher h-list'>
				
					<li class='s-search'><a class="fas fa-search fa-fw" href='javascript:void(0)'></a></li>
				
				<li class='s-menu'><a class="fas fa-bars fa-fw" href='javascript:void(0)'></a></li>
			</ul>
		</div>

		<div class='nav-sub container container--flex'>
			<a class="logo flat-box"></a>
			<ul class='switcher h-list'>
				<li class='s-comment'><a class="flat-btn fas fa-comments fa-fw" href='javascript:void(0)'></a></li>
        
          <li class='s-toc'><a class="flat-btn fas fa-list fa-fw" href='javascript:void(0)'></a></li>
        
			</ul>
		</div>
	</div>
</header>
	<aside class="menu-phone">
    <header>
		<nav class="menu navgation">
      <ul>
        
          
            <li>
							<a class="nav flat-box" href="/blog/"
                
                
                id="blog">
								<i class='fas fa-clock fa-fw'></i>&nbsp;近期文章
							</a>
            </li>
          
            <li>
							<a class="nav flat-box" href="/blog/blog/archives/"
                
                  rel="nofollow"
                
                
                id="blogblogarchives">
								<i class='fas fa-archive fa-fw'></i>&nbsp;文章归档
							</a>
            </li>
          
            <li>
							<a class="nav flat-box" href="/blog/projects/"
                
                
                id="blogprojects">
								<i class='fas fa-code-branch fa-fw'></i>&nbsp;开源项目
							</a>
            </li>
          
            <li>
							<a class="nav flat-box" href="/blog/friends/"
                
                  rel="nofollow"
                
                
                id="blogfriends">
								<i class='fas fa-link fa-fw'></i>&nbsp;我的友链
							</a>
            </li>
          
            <li>
							<a class="nav flat-box" href="https://xaoxuu.com/wiki/material-x/"
                
                  rel="nofollow"
                
                
                id="https:xaoxuu.comwikimaterial-x">
								<i class='fas fa-book fa-fw'></i>&nbsp;主题文档
							</a>
            </li>
          
            <li>
							<a class="nav flat-box" href="/blog/about/"
                
                  rel="nofollow"
                
                
                id="blogabout">
								<i class='fas fa-info-circle fa-fw'></i>&nbsp;关于小站
							</a>
            </li>
          
       
      </ul>
		</nav>
    </header>
	</aside>
<script>setLoadingBarProgress(40);</script>

  </div>


  <div class="l_body">
    <div class='body-wrapper'>
      <div class='l_main'>
  

  <article id="post" class="post white-box article-type-post" itemscope itemprop="blogPost">
    


  <section class='meta'>
    
    
    <div class="meta" id="header-meta">
      
        
  
    <h1 class="title">
      <a href="/blog/2019/11/15/Java%E7%AC%94%E8%AE%B0%E2%80%94%E5%AF%B9%E8%B1%A1%E3%80%81%E7%B1%BB/">
        Java笔记—对象、类
      </a>
    </h1>
  


      
      <div class='new-meta-box'>
        
          
        
          
            
  <div class='new-meta-item author'>
    
      <a href="https://melodyjerry.gitee.io/blog" rel="nofollow">
        
          <i class="fas fa-user" aria-hidden="true"></i>
        
        <p>Melody Jerry</p>
      </a>
    
  </div>


          
        
          
            <div class="new-meta-item date">
  <a class='notlink'>
    <i class="fas fa-calendar-alt" aria-hidden="true"></i>
    <p>2019-11-15</p>
  </a>
</div>

          
        
          
            

          
        
          
            
  
    <div class="new-meta-item browse busuanzi">
      <a class='notlink'>
        <i class="fas fa-eye" aria-hidden="true"></i>
        <p>
          <span id="busuanzi_value_page_pv">
            <i class="fas fa-spinner fa-spin fa-fw" aria-hidden="true"></i>
          </span>
        </p>
      </a>
    </div>
  


          
        
          
            
  
    <div class="new-meta-item wordcount">
      <a class='notlink'>
        <i class="fas fa-keyboard" aria-hidden="true"></i>
        <p>字数统计:</p>
        <p>5.9k字</p>
      </a>
    </div>
    <div class="new-meta-item readtime">
      <a class='notlink'>
        <i class="fas fa-hourglass-half" aria-hidden="true"></i>
        <p>阅读时长≈</p>
        <p>21分</p>
      </a>
    </div>
  

          
        
          
            

          
        
      </div>
      
        <hr>
      
    </div>
  </section>


    <section class="article typo">
      <div class="article-entry" itemprop="articleBody">
        <h3 id="Java笔记——对象、类"><a href="#Java笔记——对象、类" class="headerlink" title="Java笔记——对象、类"></a>Java笔记——对象、类</h3><p>创建时间：2019.11.11</p>
<p>说明：持续更新ing</p>
<hr>
<ol>
<li><strong><code>对象</code></strong>和<strong><code>类</code></strong>的概念。</li>
</ol>
<ul>
<li><blockquote>
<p><code>对象</code>：对象是类的一个实例（实实在在存在的个体），有<strong>属性（状态）</strong>和<strong>行为</strong>。例如，一条狗是一个对象，它的状态有：颜色、名字、品种；行为有：摇尾巴、叫、吃等。</p>
<p>属性(property)： 静态特征<br>行为(behavior)： 动态特征</p>
</blockquote>
</li>
<li><blockquote>
<p><code>类</code>：类是对现实生活中事物的描述。类是一个模板，它描述一类对象的行为和状态。<strong>同种类型对象共性的抽象</strong></p>
</blockquote>
</li>
</ul>
<a id="more"></a>

<ul>
<li><p>用老师的话来说，就是下面这样——&gt;<br>  <img src="https://i.loli.net/2019/11/16/fB95MSm8EWJD6IV.png" alt="image.png"></p>
</li>
<li><p>下图中<strong>男孩（boy）</strong>、<strong>女孩（girl）</strong>为<strong>类（class）</strong>，而具体的每个人为该类的<strong>对象（object）</strong>：<br>  <img src="https://i.loli.net/2019/11/12/P8M5LWHdIcs42yB.png" alt="菜鸟教程.png"></p>
</li>
</ul>
<hr>
<ol start="2">
<li>【定义类】</li>
</ol>
<p><img src="C:%5CUsers%5C%E4%B8%B6Melody%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20191116135106458.png" alt="image-20191116135106458"></p>
<hr>
<ol start="3">
<li></li>
</ol>
<ul>
<li>Java中的类<br>  类可以看成是创建Java对象的模板。<br>  通过下面一个简单的类来理解下Java中类的定义：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Student</span> </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> id; </span><br><span class="line">    String name; </span><br><span class="line">    <span class="keyword">double</span> score1, score2, score3; </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Student</span><span class="params">(<span class="keyword">int</span> id, String name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.id = id; </span><br><span class="line">        <span class="keyword">this</span>.name = name; </span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">getAverageScore</span><span class="params">()</span> </span>&#123; </span><br><span class="line">        <span class="keyword">return</span> (score1 ＋ score2 + score3) / <span class="number">3</span>; </span><br><span class="line">    &#125; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>一个类可以包含以下类型变量：</p>
<blockquote>
<p><code>局部变量</code>：在方法、构造方法或者语句块中定义的变量被称为局部变量。变量声明和初始化都是在方法中，方法结束后，变量就会自动销毁。<br><code>成员变量</code>：成员变量是定义在类中，方法体之外的变量。这种变量在创建对象的时候实例化。成员变量可以被类中方法、构造方法和特定类的语句块访问。<br><code>类变量</code>：类变量也声明在类中，方法体之外，但必须声明为static类型。<br>一个类可以拥有多个方法。</p>
</blockquote>
<ul>
<li>Java中的对象<br>现在让我们深入了解什么是对象。看看周围真实的世界，会发现身边有很多对象，车，狗，人等等。所有这些对象都有自己的状态和行为。<br>拿一条狗来举例，它的状态有：名字、品种、颜色，行为有：叫、摇尾巴和跑。<br>对比现实对象和软件对象，它们之间十分相似。</li>
</ul>
<p><strong><em>软件对象也有状态和行为。软件对象的状态就是属性，行为通过方法体现。<br>在软件开发中，方法操作对象内部状态的改变，对象的相互调用也是通过方法来完成。</em></strong></p>
<ul>
<li><p>创建对象——&gt;</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*有两种基本形式*/</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*1.对象声明和创建*/</span></span><br><span class="line">类名 对象名 = <span class="keyword">new</span> 类名();</span><br><span class="line">Student xiaoming = <span class="keyword">new</span> Student ();</span><br><span class="line"></span><br><span class="line"><span class="comment">/*2.先声明,后创建*/</span></span><br><span class="line">类名 对象名; <span class="comment">// 类(对象)变量</span></span><br><span class="line">对象名 = <span class="keyword">new</span> 类名(); <span class="comment">//实例化</span></span><br><span class="line">Student xiaobai ;</span><br><span class="line">xiaobai = <span class="keyword">new</span> Student();</span><br></pre></td></tr></table></figure>
</li>
<li><p><img src="https://i.loli.net/2019/11/16/ZPBq1pyImXLnwvV.png" alt="java对象内存映射像示意.png"></p>
</li>
<li><p><img src="https://i.loli.net/2019/11/16/OaWtx2fr4o53ZwH.png" alt="image.png"></p>
</li>
</ul>
<hr>
<ol start="4">
<li>类和对象的关系：<strong>类是具有相同==属性和方法==的集合，是对对象的抽象描述。</strong></li>
</ol>
<ul>
<li><p>现实生活中的对象：张三、李四。</p>
</li>
<li><p>【想要描述】提取对象中的共性内容。<strong>对具体的抽象。</strong></p>
</li>
<li><p>【描述时】这些对象的共性有：姓名、学号、年龄、性别、学习Java等等。（即<strong>定义类：描述事物的属性和行为，就是在定义属性和行为。<em>属性和行为共同成为类中的成员（成员变量和成员方法）。</em></strong>）</p>
</li>
<li><p>映射到Java中，<strong>描述就是class定义的类，具体对象就是new建立的实体</strong>。</p>
</li>
<li><p>这些描述在Java当中是用类的形式来体现的。而对象是通过Java的new操作符所产生的一个实体，这个的实体存在于==堆内存==当中。</p>
  <img src="https://i.loli.net/2019/11/13/znyBu9gp1jtdEFs.png" alt="image.png" style="zoom:80%;" />

  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//定义类，就是描述事物，就是在定义属性和行为。属性和行为共同成为类中的成员（成员变量和成员方法）。</span></span><br><span class="line"><span class="comment">//需求：描述车汽车（颜色，轮胎数）。</span></span><br><span class="line"><span class="class"><span class="keyword">class</span>	<span class="title">Car</span> </span>&#123;</span><br><span class="line">    <span class="comment">//描述颜色</span></span><br><span class="line">    String color = <span class="string">"red"</span>;</span><br><span class="line">    <span class="comment">//描述轮胎数</span></span><br><span class="line">    <span class="keyword">int</span> num = <span class="number">4</span>;</span><br><span class="line">    <span class="comment">//运行行为。</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(color+<span class="string">".."</span>+num);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span>	<span class="title">CarDemo</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        <span class="comment">//生产汽车：在Java中通过new操作符来完成。</span></span><br><span class="line">        <span class="comment">//其实就是在堆内存中产生一个实体。</span></span><br><span class="line">        Car c = <span class="keyword">new</span> Car();	<span class="comment">//c就是一个【类类型】变量。记住：类类型变量只想对象。</span></span><br><span class="line">        <span class="comment">//需求：将已有车的颜色的颜色改成蓝色。指定该对象做使用。</span></span><br><span class="line">        <span class="comment">//在Java指挥方式是：【对象.对象成员】</span></span><br><span class="line">        c.color = <span class="string">"blue"</span>;</span><br><span class="line">        <span class="comment">//需求：想要车运行起来。</span></span><br><span class="line">        c.run();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</li>
</ul>
<p><code>Car c = new Car();</code>在内存中的示意图——&gt;</p>
<img src="https://i.loli.net/2019/11/13/1X4VFtqIwDN5Epx.png" alt="image.png" style="zoom:80%;" />

<p><code>c.run();</code>——&gt;输出这个车的颜色“蓝色”和轮胎数“4”。</p>
<p>(1)倘若在<code>c.run();</code>后加入<code>Car c1=new Car();c1.run();</code>——&gt;</p>
<img src="https://i.loli.net/2019/11/13/9TSkMDwUR2z4ILe.png" alt="image.png" style="zoom:80%;" />

<p>(2)若main的代码块改为：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span>	<span class="title">CarDemo</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">		Car c = <span class="keyword">new</span> Car();</span><br><span class="line">		c.num = <span class="number">5</span>;</span><br><span class="line">		Car c1 = c;</span><br><span class="line">		c1.color = <span class="string">"green"</span>;</span><br><span class="line">		c.run();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在内存中的示意图——&gt;</p>
<img src="C:\Users\丶Melody\AppData\Roaming\Typora\typora-user-images\image-20191113105054592.png" alt="image-20191113105054592" style="zoom:80%;" />

<blockquote>
<p>对对象调用的基本概念：用new建立对象，在堆内存中产生对象。那么<strong>对象的特点在于封装数据，数据包含属性和行为</strong>。想要操作对象中的内容就指挥对象做事，指挥哪个对象要明确，操作什么也要明确，用<strong><code>对象.xyz</code></strong>的形式就能完成。</p>
</blockquote>
<p>【注意】：<br>如下图，</p>
<img src="https://i.loli.net/2019/11/13/PkjShzpB5GDg3On.png" alt="image.png" style="zoom:80%;" />

<p>以前定义在函数里面，现在定义在函数外面                 </p>
<hr>
<ol start="5">
<li><p>【方法】<br> 方法的定义：</p>
<ul>
<li><p><strong>方法是类或对象的行为特征的抽象。</strong></p>
</li>
<li><p>Java中的方法不能独立存在，所有的方法必须定义在类中。</p>
</li>
<li><p>使用 “类名.方法” 或 “对象.方法” 的形式调用。</p>
</li>
<li><p>语法格式：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">权限修饰符　返回值类型　方法名（参数类型　参数名<span class="number">1</span>,参数类型　参数名<span class="number">2</span>,...） &#123;</span><br><span class="line">	<span class="comment">// 方法体</span></span><br><span class="line">	方法语句序列<span class="comment">//当返回类型非void时,最后一条被执行语句必须是return语句,返回对应类型的值</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>访问权限/权限修饰符（4P权限）：</p>
<table>
<thead>
<tr>
<th align="center">访问权限</th>
<th align="center">类内</th>
<th align="center">包层内</th>
<th align="center">子类</th>
<th align="center">包层外</th>
</tr>
</thead>
<tbody><tr>
<td align="center">public</td>
<td align="center">√</td>
<td align="center">√</td>
<td align="center">√</td>
<td align="center">√</td>
</tr>
<tr>
<td align="center">protected</td>
<td align="center">√</td>
<td align="center">√</td>
<td align="center">√</td>
<td align="center">×</td>
</tr>
<tr>
<td align="center">package （缺省）</td>
<td align="center">√</td>
<td align="center">√</td>
<td align="center">×</td>
<td align="center">×</td>
</tr>
<tr>
<td align="center">private</td>
<td align="center">√</td>
<td align="center">×</td>
<td align="center">×</td>
<td align="center">×</td>
</tr>
</tbody></table>
<p><code>return</code>语句：</p>
</li>
<li><p>结束方法调用，并返回值。</p>
</li>
<li><table>
<thead>
<tr>
<th>return 表达式;</th>
<th>return;</th>
</tr>
</thead>
<tbody><tr>
<td>🔺方法被调用时,执行到方法内的return语句,则不论方法里是否还有语句未执行,立即结束方法的调用,并将return 后的表达式的值返回方法调用者<br />🔺利用return 语句返回值，仅能返回一个值<br />🔺return语句仅结束方法的执行</td>
<td>🔺void型方法</td>
</tr>
</tbody></table>
<p>方法分类：</p>
</li>
<li><p>无参数无返回值</p>
</li>
<li><p>有参数无返回值</p>
</li>
<li><p>无参数有返回值</p>
</li>
<li><p>有参数有返回值</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Method</span> </span>&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">aMethod</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"无参数无返回值的方法"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">bMethod</span><span class="params">(<span class="keyword">int</span> b)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"有参数无返回值的方法"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">cMethod</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"无参数有返回值的方法"</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">10</span>;</span><br><span class="line">    &#125;</span><br><span class="line">   </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">dMethod</span><span class="params">(<span class="keyword">int</span> d)</span> </span>&#123;</span><br><span class="line">       System.out.println(<span class="string">"有参数有返回值的方法"</span>);</span><br><span class="line">        <span class="keyword">return</span> d;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> ret;　　　　　<span class="comment">// 创建Method类的对象</span></span><br><span class="line">       Method md = <span class="keyword">new</span> Method();　　　　　<span class="comment">// 通过对象.方法调用</span></span><br><span class="line">        md.aMethod();</span><br><span class="line">       md.bMethod(<span class="number">10</span>);</span><br><span class="line">        ret = md.cMethod();</span><br><span class="line">       ret = md.dMethod(<span class="number">10</span>);</span><br><span class="line">        System.out.println(ret);</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>方法的参数：</p>
</li>
<li><p>方法可以没有参数，或者有多个参数，参数类型可以是任意类型</p>
</li>
<li><p>方法参数也是局部变量</p>
<p>  参数为<a href="https://www.cnblogs.com/wuxinyiwu/p/8588420.html" target="_blank" rel="noopener">引用数据类型</a><a href="[JAVA中基本数据类型和引用数据类型区别](https://www.cnblogs.com/wuxinyiwu/p/8588420.html)">^4</a>时：</p>
</li>
<li><p>当对象实例作为参数传递给方法时，传递的是对象的引用，为地址传递，接受参数的方法可以改变参数的值。</p>
<p>参数为简单数据类型时：</p>
</li>
<li><p>传递的是参数的副本，为值传递，接受参数的方法中不会改变参数的值。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MethodParam</span> </span>&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 方法的参数为基本数据类型时，传递的是值的副本（值拷贝）</span></span><br><span class="line"><span class="comment">     * 方法中不会改变元参数的值</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">swap</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span> </span>&#123;<span class="comment">//a, b为形参</span></span><br><span class="line">        <span class="keyword">int</span> tmp;</span><br><span class="line">        tmp = a;</span><br><span class="line">        a = b;</span><br><span class="line">        b = tmp;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">int</span> x = <span class="number">100</span>, y = <span class="number">200</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 方法的参数为引用数据类型时，传递的对象的引用（传地址）</span></span><br><span class="line"><span class="comment">     * 方法中可以改变参数的值</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">swap2</span><span class="params">(MethodParam mp)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> tmp = mp.x;</span><br><span class="line">        mp.x = mp.y;</span><br><span class="line">        mp.y = tmp;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        MethodParam mp = <span class="keyword">new</span> MethodParam();</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">int</span> m = <span class="number">10</span>, n = <span class="number">20</span>;</span><br><span class="line">        System.out.println(<span class="string">"交换前：a = "</span>+m+<span class="string">",b = "</span>+n);</span><br><span class="line">        mp.swap(m, n);<span class="comment">// m,n为实参</span></span><br><span class="line">        System.out.println(<span class="string">"交换后：a = "</span>+m+<span class="string">",b = "</span>+n);</span><br><span class="line">        </span><br><span class="line">        System.out.println(<span class="string">"交换前：x = "</span>+mp.x+<span class="string">",y = "</span>+mp.y);</span><br><span class="line">        mp.swap2(mp);</span><br><span class="line">        System.out.println(<span class="string">"交换后：x = "</span>+mp.x+<span class="string">",y = "</span>+mp.y);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>普通方法调用——&gt;</p>
<p><img src="https://i.loli.net/2019/11/16/28HpyTLnKOVsFQY.png" alt="image.png"></p>
<p>成员方法和成员变量的关系——&gt;</p>
<p><img src="https://i.loli.net/2019/11/16/gceJWarS2Puqz9w.png" alt="image.png"></p>
</li>
</ul>
</li>
</ol>
<hr>
<ol start="6">
<li><p>【成员方法】和【构造方法】</p>
<p> 【成员方法】</p>
</li>
</ol>
<pre><code>——————————————————————————— 我 是 分 割 线 ————————————————————————————

【构造方法】

构造方法需满足以下三个条件 ：</code></pre><ul>
<li><p>1、方法名与类名<strong>同名</strong></p>
</li>
<li><p>2、在方法中不能使用  <strong><code>return</code></strong>  语句，即无返回值 </p>
</li>
<li><p>3、在方法名的前面*<em>没有返回值的类型声明 *</em><br>  （有看到“ 构造方法无返回值也不可以加void，但其实返回的是首地址 ”这话还得去证实，先放在这里）</p>
</li>
<li><p><img src="https://i.loli.net/2019/11/16/qIK3BjS4wrtvgMi.png" alt="image.png"></p>
</li>
<li><blockquote>
<p>==老师推荐==：<strong><em>显式无参构造子,作为一种良好实践,定义了其他构造子，那就准备一个无参构造子</em></strong></p>
</blockquote>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> me;</span><br><span class="line"><span class="keyword">public</span>  <span class="class"><span class="keyword">class</span> <span class="title">Me</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span>  String name; <span class="comment">//成员变量</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Me</span><span class="params">(String name)</span></span>&#123; <span class="comment">//构造方法</span></span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123; <span class="comment">//成员方法</span></span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;<span class="comment">//成员方法</span></span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p> 我如果我在添加一age 如下 添加方法依然是成员方法 , </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> me;</span><br><span class="line"><span class="keyword">public</span>  <span class="class"><span class="keyword">class</span> <span class="title">Me</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span>  String name; <span class="comment">//成员变量</span></span><br><span class="line">    <span class="keyword">private</span> String age; <span class="comment">//成员变量</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Me</span><span class="params">(String name)</span></span>&#123; <span class="comment">//构造方法</span></span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123; <span class="comment">//成员方法</span></span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;<span class="comment">//成员方法</span></span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getAge</span><span class="params">()</span> </span>&#123;<span class="comment">//成员方法</span></span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setAge</span><span class="params">(String age)</span> </span>&#123;<span class="comment">//成员方法</span></span><br><span class="line">        <span class="keyword">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>个人理解：无论多少方法，==除了构造方法外，其余都是成员方法==，当然这样理解可能有点偏见..</p>
<p>它们的区别：</p>
<ul>
<li><p>构造方法一般是给对象的数据进行初始化,没有返回值</p>
</li>
<li><p>成员方法一般实现对类中成员变量的操作，提供某些功能,有返回类型，可以为void类型</p>
</li>
</ul>
<p>成员方法与构造方法调用的区别</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> me;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Book</span> </span>&#123;    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;        </span><br><span class="line">        Me me = <span class="keyword">new</span> Me(<span class="string">"张三"</span>);<span class="comment">//构造方法调用    </span></span><br><span class="line">        System.out.println(me);         </span><br><span class="line">        me.setName(<span class="string">"李四"</span>);<span class="comment">//成员方法调用       </span></span><br><span class="line">        System.out.println(me.getName());     </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>从上面的demo中可以看出，<strong>==构造方法通过new运算符调用==</strong>，<strong>==成员方法通过对象调用==</strong>。</p>
<p><strong>注意：类中除了构造方法以外的方法都是成员方法。原因是在类中，除了变量外的基本都是成员方法。</strong></p>
<p>——————————————————————————— 我 是 分 割 线 ————————————————————————————</p>
<p>【this 关键字】</p>
<p><a href="https://i.loli.net/2019/11/16/49d7AMODXhKbsuy.png" target="_blank" rel="noopener">https://i.loli.net/2019/11/16/49d7AMODXhKbsuy.png</a> </p>
<p>——————————————————————————— 我 是 分 割 线 ————————————————————————————</p>
<p>【重载构造方法之间的调用】</p>
<p><img src="https://i.loli.net/2019/11/16/6qrpXmf5a3bhojz.png" alt="image.png"></p>
<p><img src="https://i.loli.net/2019/11/16/pXLMjyKFzgal1oB.png" alt="image.png"></p>
<p>【含对象成员的构造方法调用】<img src="https://i.loli.net/2019/11/16/Awpe2iSNX4OnvaQ.png" alt="image.png"></p>
<p><img src="https://i.loli.net/2019/11/16/49d7AMODXhKbsuy.png" alt="image.png"> </p>
<p>【无继承情况下的对象初始化顺序】<img src="https://i.loli.net/2019/11/16/AO3z8orS9aXvWCd.png" alt="image.png"></p>
<hr>
<ol start="7">
<li>【创建对象】</li>
</ol>
<p>对象是根据类创建的。在Java中，使用<strong>关键字new</strong><a href="[java之new的用处](https://blog.csdn.net/weixin_42227243/article/details/82805945)">^3</a>:来创建一个新的对象。创建对象需要以下三步：</p>
<ul>
<li><strong>声明</strong>：声明一个对象，包括对象名称和对象类型。</li>
<li><strong>实例化</strong>：使用关键字new来创建一个对象。</li>
<li><strong>初始化</strong>：使用new创建对象时，会调用构造方法初始化对象。</li>
</ul>
<p>下面是一个创建对象的例子：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Puppy</span></span>&#123;   </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Puppy</span><span class="params">(String name)</span></span>&#123;      </span><br><span class="line">        <span class="comment">//这个构造器仅有一个参数：name      </span></span><br><span class="line">        System.out.println(<span class="string">"小狗的名字是 : "</span> + name );    </span><br><span class="line">    &#125;   </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;      </span><br><span class="line">        <span class="comment">// 下面的语句将创建一个Puppy对象      </span></span><br><span class="line">        Puppy myPuppy = <span class="keyword">new</span> Puppy( <span class="string">"tommy"</span> );   </span><br><span class="line">    &#125; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译并运行上面的程序，会打印出下面的结果：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">小狗的名字是 : tommy</span><br></pre></td></tr></table></figure>

<p>——————————————————————————— 我 是 分 割 线 ————————————————————————————</p>
<p>【访问实例变量和方法】</p>
<p>通过已创建的对象来访问成员变量和成员方法，如下所示：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 实例化对象 */</span> </span><br><span class="line">Object referenceVariable = <span class="keyword">new</span> Constructor(); </span><br><span class="line"><span class="comment">/* 访问类中的变量 */</span> </span><br><span class="line">referenceVariable.variableName; </span><br><span class="line"><span class="comment">/* 访问类中的方法 */</span> </span><br><span class="line">referenceVariable.methodName();</span><br></pre></td></tr></table></figure>

<p>——————————————————————————— 我 是 分 割 线 ————————————————————————————</p>
<p>【访问实例变量】和【调用成员方法】：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Puppy</span></span>&#123;   </span><br><span class="line">    <span class="keyword">int</span> puppyAge;   </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Puppy</span><span class="params">(String name)</span></span>&#123;      </span><br><span class="line">        <span class="comment">// 这个构造器仅有一个参数：name      </span></span><br><span class="line">        System.out.println(<span class="string">"小狗的名字是 : "</span> + name );    </span><br><span class="line">    &#125;    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setAge</span><span class="params">( <span class="keyword">int</span> age )</span></span>&#123;       </span><br><span class="line">        puppyAge = age;   </span><br><span class="line">    &#125;    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getAge</span><span class="params">( )</span></span>&#123;       </span><br><span class="line">        System.out.println(<span class="string">"小狗的年龄为 : "</span> + puppyAge );        			<span class="keyword">return</span> puppyAge;   </span><br><span class="line">    &#125;   </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;     </span><br><span class="line">        <span class="comment">/* 创建对象 */</span>      </span><br><span class="line">        Puppy myPuppy = <span class="keyword">new</span> Puppy( <span class="string">"tommy"</span> );      </span><br><span class="line">        <span class="comment">/* 通过方法来设定age */</span>    </span><br><span class="line">        myPuppy.setAge( <span class="number">2</span> );      </span><br><span class="line">        <span class="comment">/* 调用另一个方法获取age */</span>    </span><br><span class="line">        myPuppy.getAge( );     </span><br><span class="line">        <span class="comment">/*你也可以像下面这样访问成员变量 */</span>   </span><br><span class="line">        System.out.println(<span class="string">"变量值 : "</span> + myPuppy.puppyAge );   </span><br><span class="line">    &#125; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译并运行上面的程序，产生如下结果：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">小狗的名字是 : tommy</span><br><span class="line">小狗的年龄为 : <span class="number">2</span></span><br><span class="line">变量值 : <span class="number">2</span></span><br></pre></td></tr></table></figure>

<hr>
<ol start="8">
<li><p>【方法重载，重构，覆盖，重写的区别】</p>
<p><strong>方法重载</strong>还是比较好理解的，就是<strong>在类的内部</strong>，定义多个方法，这些方法的方法名字相同，参数类型、参数顺序、参数个数不同，注意，返回值类型并不能区分重载 。</p>
</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">show</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">double</span> b)</span></span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">show</span><span class="params">(<span class="keyword">double</span> c)</span></span>&#123;</span><br><span class="line"></span><br><span class="line">&#125; </span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">show</span><span class="params">(<span class="keyword">double</span> d)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span>  <span class="number">1</span>;</span><br><span class="line">&#125;   <span class="comment">//报错，方法已经定义了</span></span><br></pre></td></tr></table></figure>

<p> <strong>方法重写（<code>override</code>)</strong><br>方法重写和方法覆盖都是一个道理，发生在子类继承父类或者实现接口的类，要求的是：</p>
<blockquote>
<p>1.方法名字相同，<br>2.参数个数不同，<br>3.返回值类型不同 ，</p>
</blockquote>
<p>代码： </p>
<img src="https://i.loli.net/2019/11/14/hMWBc8wnbZr7PtG.png" alt="image.png"  />



<img src="https://i.loli.net/2019/11/14/5bCilLKw1uFWEGy.png" alt="image.png"  />

<p> <strong>方法重构</strong></p>
<blockquote>
<p>定义：在不改变外界外界访问的情况下对代码进行修改，但是不改变原方法的效果，提高其性能，使程序更加易读，可维护。<br>重构是在不改变软件可观察行为的前提下改善其内部结构<br>重构通常不是一次性的，它贯穿软件的整个生命周期，只要觉得不合理都是重构的时机。 </p>
</blockquote>
<hr>
<ol start="9">
<li></li>
</ol>
<p><strong>题目：</strong>下面说法中，错误的有（  ）</p>
<p>​            A. Java面向对象语言容许单独的过程与函数存在；</p>
<p>​            B. Java面向对象语言容许单独的方法存在；</p>
<p>​            C. Java语言中的方法属于类中的成员（member）；</p>
<p>​            D. Java语言中的方法必定隶属于某一类（对象），调用方法与过程或函数相同。</p>
<p> 答案：ABC</p>
<p> 解释：</p>
<blockquote>
<p>方法与函数：这两个概念不严格区分，就是一个东西，通过对象调用的，就是方法，直接调用，就是函数；</p>
<p>在java中，everything is object 所以，方法，函数，必须隶属于某一个类或对象， java中没有过程。（类中的代码块不知道算不算，就算代码块是过程，也是必须在类中才有的）</p>
<p>对于C选项，方法分为静态方法和非静态方法，静态方法属于类成员，非静态方法属于实例成员。</p>
</blockquote>
<hr>
<ol start="10">
<li></li>
</ol>
<ul>
<li><p>类其实一种类型，它封装了数据和操作。</p>
</li>
<li><p>对象是类的实例</p>
</li>
<li><p>一个对象比属于某个类</p>
</li>
<li><p>一个类可以声明多个对象</p>
</li>
</ul>
<hr>
<ol start="11">
<li>问：构造方法可以调用本类的其他方法吗？</li>
</ol>
<blockquote>
<p>1,构造方法可以调用本类的其他方法。既然可以调用本类的属性,那么也就可以调用属于本类的其他方法。</p>
</blockquote>
<blockquote>
<p>2,需要注意的是,如果要在构造方法里调用一个非静态方法时,应该将该方法声明为private。<br>因为如果这个类被其他类继承成为父类时,这个方法不是private的又被子类所重载，这样在实际创建子类的过程中递归调用到了父类的构造器时，父类构造器对这个方法的调用就会由于多态而实际上调用了子类的方法，当这个子类方法需要用到子类中实例变量<a href="[实例变量（全局变量）](https://www.jianshu.com/p/08562e95ed49)、[成员变量、实例变量、属性](https://www.cnblogs.com/huangzs/p/9961703.html)">^1</a>的时候，就会由于变量没有初始化而出现异常（子类中的实例变量没有初始化这种情况还是会遇见的），这是Java不想看到的情况。 </p>
</blockquote>
<hr>
<ol start="12">
<li>一个类的构造方法有多个，在其他类中是否可以被调用？</li>
</ol>
<blockquote>
<p> 只要不是私有的构造函数，在其他类中就可以调用。 传对应的参数就行了 。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span>` `<span class="title">A</span></span>&#123;</span><br><span class="line">  ``<span class="keyword">public</span>` `A(String s)&#123;&#125;</span><br><span class="line">  ``<span class="keyword">public</span>` `A(``<span class="keyword">int</span>` `n)&#123;&#125;</span><br><span class="line">  ``<span class="keyword">public</span>` `A(String s, ``<span class="keyword">int</span>` `n)&#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span>` `<span class="title">B</span></span>&#123;</span><br><span class="line">  ``A a1 = ``<span class="keyword">new</span>` `A(``<span class="string">"abc"</span>``);</span><br><span class="line">  ``A a2 = ``<span class="keyword">new</span>` `A(``<span class="number">1</span>``);</span><br><span class="line">  ``A a3 = ``<span class="keyword">new</span>` `A(``<span class="string">"aaa"</span>``,``<span class="number">2</span>``);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</blockquote>
<hr>
<ol start="13">
<li>类是一种<strong>引用类型</strong>。</li>
</ol>
<p>Java的引用类型只有三种，分别是</p>
<ul>
<li><p><strong>类(class)</strong></p>
</li>
<li><p><strong>接口(interface)</strong></p>
</li>
<li><p><strong>数组</strong></p>
<p>  <a href="https://blog.csdn.net/aEzreal/article/details/80822062" target="_blank" rel="noopener">参考资料</a></p>
</li>
</ul>
<hr>
<ol start="14">
<li>问：方法一定是在类内部被定义的？</li>
</ol>
<blockquote>
<p> 所有方法必须写在类体里，也包括构造方法<a href="[java的构造方法](https://www.jianshu.com/p/773a8e5e199c)、[java的构造方法](https://blog.csdn.net/qq_40393000/article/details/82866470)、[Java入门：构造方法](https://blog.csdn.net/weixin_30455365/article/details/95613213)">^2</a>的。</p>
<p> static方法同样属于类，叫类方法或静态方法，它与类一同被加载时，处于内存的公共代码区中，无需创建实例来调用，只需用类名引用即可。<br> static方法既可以通过类名来调用，也可以通过类的实例调用。</p>
<p> Java一切都是以类的形式存在，方法是类的一部分，需要通过类才能进行方法的调用。<br> 从内存存储角度来说，java中找对应方法时，都是先找对应类实例，类实例是根据类来初始化内存的。如果方法在类外部，则无法找到该方法。<br> 所以方法必须在类体里。 </p>
<p> 注意： 可以在其他类里使用这个类的方法（在不同的包下就要引ltem这个类），继承就是一个类从另一个类继承通过关键字extends 来实现 </p>
</blockquote>
<hr>
<ol start="15">
<li>问：构造方法中可不可以有return语句呢?</li>
</ol>
<blockquote>
<p> （这个解释有问题？）可以。而是我们写成这个样子就OK了：<code>return;</code><br> 其实，在任何的<strong>void类型的方法</strong>的最后你都可以写上：<code>return;</code></p>
</blockquote>
<hr>
<ol start="16">
<li>问：非构造方法的一般成员方法必须有return语句？</li>
</ol>
<blockquote>
<p>错。 （待补充……）</p>
</blockquote>
<hr>
<ol start="17">
<li>问：类中的数据成员可以按需选择任意类型，包括类本身？</li>
</ol>
<blockquote>
<p>可以。可以参考下图，图源于<a href="https://blog.csdn.net/weixin_38088772/article/details/91041309" target="_blank" rel="noopener">陈三千的博客</a></p>
</blockquote>
<img src="https://img-blog.csdnimg.cn/20190606111353386.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODA4ODc3Mg==,size_16,color_FFFFFF,t_70" alt="举例.png"  />

<hr>
<ol start="18">
<li>问：Java中构造函数的权限可以是private吗？</li>
</ol>
<blockquote>
<p> 可以，这个类就不能再外部被new了，如果需要这个类的实例化对象，就只能在类内部提供静态方法生产这个类的对象了。 </p>
</blockquote>
<hr>
<ol start="19">
<li>问：构造方法能不能被重载，构造方法能不能被重写？</li>
</ol>
<blockquote>
<p>在回答这两个问题之前，首先先回顾：<strong>什么是构造方法？构造方法的特点是什么？</strong></p>
<blockquote>
<p>概述：构造方法存在于类中，给对象数据（属性）初始化；</p>
<p>特点：方法名与类名一样；无返回值无void;</p>
<p>默认构造方法：我们不创建一个构造方法时，系统默认提供一个无参构造；当我们创建一个构造方法时，系统不再提供无参构造，所以在实际项目中，全部手动给出无参构造</p>
</blockquote>
<p>其次<strong><em>\</em>什么是重载？什么是重写？**</strong> </p>
<blockquote>
<p>重载：存在于在一个类中，方法名相同，方法参数的类型或个数不同</p>
<p>重写：存在于子父类中，方法名、方法参数、返回值全部相同</p>
</blockquote>
<p>所以：<strong>构造方法可以重载，不能重写</strong></p>
<p>在一个类中，可以有多个构造方法（方法参数不同） ，即重载，来实现对象属性不同的初始化；</p>
<p>但是子类中不能定义一个方法无void无返回值的方法，编译错误，即子类无法继承构造方法，但是子类的构造器中可以调用父类的构造方法（默认自动调用无参构造）</p>
</blockquote>
<p>注意：回答来源于 <a href="https://blog.csdn.net/cherry_11qianqian/article/details/82785893" target="_blank" rel="noopener">CSDN博客</a></p>
<hr>
<ol start="20">
<li>Java中可以定义一个<strong>空类</strong>（即没有数据成员和成员方法） 。</li>
</ol>
<hr>
<ol start="21">
<li>问： 下面这个类有几个构造方法？ </li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">class	A&#123;&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>1个。（待补充……）</p>
</blockquote>
<hr>
<ol start="22">
<li><p>问：Java 类可以作为（ C ）</p>
<p>A．类型定义机制                                 B．数据封装机制</p>
<p>C．类型定义机制和数据封装机制      D．上述都不对</p>
</li>
</ol>
<hr>
<ol start="23">
<li>用于定义类头的修饰符可以是public、abstract、final或者是默认(friendly) </li>
</ol>
<hr>
<ol start="24">
<li>题目：下列说法错误的有（ CD ）<br>　　A． 在类方法中可用this来调用本类的类方法<pre><code>B． 在类方法中调用本类的类方法时可直接调用</code></pre>　　C． 在类方法中只能调用本类中的类方法<br>　　D． 在类方法中绝对不能调用实例方法 </li>
</ol>
<blockquote>
<p> C：类方法可以调用外部其他类的方法。<br>D：只要实力化对象，也是可以调用实例方法的。</p>
</blockquote>
<hr>
<ol start="25">
<li>判断： 只有基本类型的参数传递使用值传递。 </li>
</ol>
<blockquote>
<p>错误。</p>
<p>引用类型参数的传递，依然是值传递。但因为是地址值，所以会指向同一个对象实例。</p>
</blockquote>
<hr>
<ol start="26">
<li>Java中方法的参数传递是<strong>值传递（pass by value）</strong>，并且为<strong>单向传递</strong>。</li>
</ol>
<hr>
<ol start="27">
<li>《Java 变量参数传入方法，修改后是否影响外面的值》。参考   <a href="https://blog.csdn.net/fenglllle/article/details/81389286" target="_blank" rel="noopener">博客</a></li>
</ol>
<hr>
<ol start="28">
<li>《某个变量当作函数的参数传递后，函数内部修改了。到底这个变量是否受影响》。参考 <a href="https://blog.csdn.net/wem603947175/article/details/81206177" target="_blank" rel="noopener">博客</a></li>
</ol>
<hr>
<ol start="29">
<li><p>问：类设计get方法和set方法的作用是什么？</p>
<blockquote>
<p>答1：一般类的成员变量被设置为private（私有的），这样设置安全性比较高，但是设置为私有的后其他的类和对象则无法调用该对象了，所以会有get和set方法，这两个方法一般设置为public，在其他的类中通过对象调用set或get方法即可操作私有的变量，增强程序的安全性，set为给对象赋值的方法，而get则是取得变量值的方法！</p>
</blockquote>
<blockquote>
<p>答2：get set常用在自定义类，类创建了一个实例对象后，用set设置对象的某一个属性，get得到对象某一属性。如果不使用get set，你就得用点，点出来还得是个方法。其实就是给get set换个名而已。而且get set作为内部方法，可以访问私有数据</p>
</blockquote>
<blockquote>
<p>答3：别说的那么专业化了，理解都要半天。<br>就这么理解吧，面向对象中，想对类的成员变量设置访问一些权限，于是，就将该字段设置为private的，这样别的类就不能访问了，然后在该内定义两个方法，一个专门用于获取，一个用于修改，这样，在你不想让别人乱动该字段的时候，就在该方法内写一些限制条件。<br>而为了规范起见（团队开发需要嘛），就根据人家老外的习惯，将方法名设置为set+字段名，get+字段名（名字乱改没问题），既然规范了，那就是别人都叫这名了，于是，有些框架给对象赋值就有方法了，方法名称由get或set开始的又有private变量名的，就找出来调用。hibernate1框架就这样弄得，至于后面的版本是不是这样就不大清楚了。<br>JavaBean就是一个JAVA类，既然是JAVA类就不需要什么组件了，这玩意直接深入到虚拟机了的</p>
</blockquote>
<p><img src="https://i.loli.net/2019/11/16/4pmW9kbx6tvFeGU.png" alt="image.png"></p>
<p><img src="https://i.loli.net/2019/11/16/maOenK4ryJWhFGI.png" alt="image.png"></p>
<p><img src="https://i.loli.net/2019/11/16/QKyFju219pVgP6l.png" alt="image.png"></p>
<p><img src="https://i.loli.net/2019/11/16/3Sa6uLv4QGhB2n8.png" alt="image.png"></p>
<p><img src="https://i.loli.net/2019/11/16/yReDOpbWKHPd8Lg.png" alt="image.png"></p>
</li>
</ol>
<hr>
<ol start="30">
<li><p>Java的包装类：</p>
<p>【包装类的概念】</p>
<p><img src="https://i.loli.net/2019/11/16/xceTj6DX23hzkwi.png" alt="image.png"></p>
<p>【Integer类的用法】</p>
<p><img src="https://i.loli.net/2019/11/16/VN1psGdfXIbwjMH.png" alt="image.png"></p>
<p><img src="https://i.loli.net/2019/11/16/hwZ1jA6agmVn374.png" alt="image.png"></p>
<p>【自动装箱机制和自动拆箱机制】</p>
<p><img src="https://i.loli.net/2019/11/16/qI9VgpAnHw3jvao.png" alt="image.png"></p>
<p><img src="https://i.loli.net/2019/11/16/YKXwTL9SGduN2cI.png" alt="image.png"></p>
</li>
</ol>
<hr>
<ol start="31">
<li>匿名对象：</li>
</ol>
<p>使用方法：</p>
<ol>
<li>当对对象的方法只调用一次，可以使用匿名对象来完成，这样写比较简化。如果对一个对象进行多个成员调用，必须给这个对象起个名字。</li>
<li>可以将匿名对象作为实际参数进行传递。</li>
<li></li>
</ol>
<hr>
<ol start="32">
<li></li>
</ol>
<hr>
<ol start="33">
<li></li>
</ol>
<hr>
<hr>
<hr>
<hr>
<hr>
<hr>
<hr>
<hr>
<hr>
<h3 id="注脚："><a href="#注脚：" class="headerlink" title="注脚："></a>注脚：</h3>
      </div>
      
      
        <br>
        


  <section class='meta' id="footer-meta">
    <div class='new-meta-box'>
      
        
          <div class="new-meta-item date" itemprop="dateUpdated" datetime="2019-11-30T18:18:12+08:00">
  <a class='notlink'>
    <i class="fas fa-clock" aria-hidden="true"></i>
    <p>更新于 2019年11月30日</p>
  </a>
</div>

        
      
        
          
  
  <div class="new-meta-item meta-tags"><a class="tag" href="/blog/tags/Java/" rel="nofollow"><i class="fas fa-tag" aria-hidden="true"></i><p>Java</p></a></div> <div class="new-meta-item meta-tags"><a class="tag" href="/blog/tags/%E7%AC%94%E8%AE%B0/" rel="nofollow"><i class="fas fa-tag" aria-hidden="true"></i><p>笔记</p></a></div>


        
      
        
          
  <div class="new-meta-item share -mob-share-list">
  <div class="-mob-share-list share-body">
    
      
        <a class="-mob-share-qq" title="QQ好友" rel="external nofollow noopener noreferrer"
          
          href="http://connect.qq.com/widget/shareqq/index.html?url=https://melodyjerry.gitee.io/blog/2019/11/15/Java%E7%AC%94%E8%AE%B0%E2%80%94%E5%AF%B9%E8%B1%A1%E3%80%81%E7%B1%BB/&title=Java笔记—对象、类 | MelodyHub&summary=Java笔记——对象、类创建时间：2019.11.11
说明：持续更新ing


对象和类的概念。



对象：对象是类的一个实例（实实在在存在的个体），有属性（状态）和行为。例如，一条狗是一个对象，它的状态有：颜色、名字、品种；行为有：摇尾巴、叫、吃等。
属性(property)： 静态特征行为(behavior)： 动态特征



类：类是对现实生活中事物的描述。类是一个模板，它描述一类对象的行为和状态。同种类型对象共性的抽象


"
          
          >
          
            <img src="https://cdn.jsdelivr.net/gh/xaoxuu/assets@19.1.9/logo/128/qq.png">
          
        </a>
      
    
      
        <a class="-mob-share-qzone" title="QQ空间" rel="external nofollow noopener noreferrer"
          
          href="https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=https://melodyjerry.gitee.io/blog/2019/11/15/Java%E7%AC%94%E8%AE%B0%E2%80%94%E5%AF%B9%E8%B1%A1%E3%80%81%E7%B1%BB/&title=Java笔记—对象、类 | MelodyHub&summary=Java笔记——对象、类创建时间：2019.11.11
说明：持续更新ing


对象和类的概念。



对象：对象是类的一个实例（实实在在存在的个体），有属性（状态）和行为。例如，一条狗是一个对象，它的状态有：颜色、名字、品种；行为有：摇尾巴、叫、吃等。
属性(property)： 静态特征行为(behavior)： 动态特征



类：类是对现实生活中事物的描述。类是一个模板，它描述一类对象的行为和状态。同种类型对象共性的抽象


"
          
          >
          
            <img src="https://cdn.jsdelivr.net/gh/xaoxuu/assets@19.1.9/logo/128/qzone.png">
          
        </a>
      
    
      
        <a class="-mob-share-weibo" title="微博" rel="external nofollow noopener noreferrer"
          
          href="http://service.weibo.com/share/share.php?url=https://melodyjerry.gitee.io/blog/2019/11/15/Java%E7%AC%94%E8%AE%B0%E2%80%94%E5%AF%B9%E8%B1%A1%E3%80%81%E7%B1%BB/&title=Java笔记—对象、类 | MelodyHub&summary=Java笔记——对象、类创建时间：2019.11.11
说明：持续更新ing


对象和类的概念。



对象：对象是类的一个实例（实实在在存在的个体），有属性（状态）和行为。例如，一条狗是一个对象，它的状态有：颜色、名字、品种；行为有：摇尾巴、叫、吃等。
属性(property)： 静态特征行为(behavior)： 动态特征



类：类是对现实生活中事物的描述。类是一个模板，它描述一类对象的行为和状态。同种类型对象共性的抽象


"
          
          >
          
            <img src="https://cdn.jsdelivr.net/gh/xaoxuu/assets@19.1.9/logo/128/weibo.png">
          
        </a>
      
    
  </div>
</div>



        
      
    </div>
  </section>


      
      
          <div class="prev-next">
              
                  <section class="prev">
                      <span class="art-item-left">
                          <h6><i class="fas fa-chevron-left" aria-hidden="true"></i>&nbsp;上一页</h6>
                          <h4>
                              <a href="/blog/2019/11/16/%E8%A7%A3%E9%99%A4%E8%AF%BE%E7%A8%8B%E4%B8%AD%E5%BF%83%E7%9C%8B%E8%A7%86%E9%A2%91%E6%97%B6%E7%9A%84%E2%80%9C%20%E7%AA%97%E5%8F%A3%E8%8E%B7%E5%8F%96%E7%84%A6%E7%82%B9%E4%BA%8B%E4%BB%B6%20%E2%80%9D/" rel="prev" title="解除课程中心看视频时的“ 窗口获取焦点事件 ”">
                                
                                    解除课程中心看视频时的“ 窗口获取焦点事件 ”
                                
                              </a>
                          </h4>
                          
                              
                              <h6 class="tags">
                                  <a class="tag" href="/blog/tags/%E8%84%9A%E6%9C%AC/"><i class="fas fa-tag fa-fw" aria-hidden="true"></i> 脚本</a>
                              </h6>
                          
                      </span>
                  </section>
              
              
                  <section class="next">
                      <span class="art-item-right" aria-hidden="true">
                          <h6>下一页&nbsp;<i class="fas fa-chevron-right" aria-hidden="true"></i></h6>
                          <h4>
                              <a href="/blog/2019/11/13/Java%E7%AC%94%E8%AE%B0%E2%80%94%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" rel="prev" title="Java笔记—面向对象">
                                  
                                      Java笔记—面向对象
                                  
                              </a>
                          </h4>
                          
                              
                              <h6 class="tags">
                                  <a class="tag" href="/blog/tags/Java/"><i class="fas fa-tag fa-fw" aria-hidden="true"></i> Java</a> <a class="tag" href="/blog/tags/%E7%AC%94%E8%AE%B0/"><i class="fas fa-tag fa-fw" aria-hidden="true"></i> 笔记</a>
                              </h6>
                          
                      </span>
                  </section>
              
          </div>
      
    </section>
  </article>



  <!-- 显示推荐文章和评论 -->



  <article class="post white-box comments">
    <section class="article typo">
      <h4><i class="fas fa-comments fa-fw" aria-hidden="true"></i>&nbsp;评论</h4>
      
      
      
      
        <section id="comments">
          <div id="valine_container" class="valine_thread">
            <i class="fas fa-spinner fa-spin fa-fw"></i>
          </div>
        </section>
      
    </section>
  </article>






<!-- 根据页面mathjax变量决定是否加载MathJax数学公式js -->



  <script>
    window.subData = {
      title: 'Java笔记—对象、类',
      tools: true
    }
  </script>


</div>
<aside class='l_side'>
  
    
    
      
      
        
          
          
            
              <section class='widget author'>
  <div class='content pure'>
    
      <div class='avatar'>
        <img class='avatar' src='https://cdn.jsdelivr.net/gh/xaoxuu/assets@master/avatar/avatar.png'/>
      </div>
    
    
      <div class='text'>
        
        
        
          <p><span id="jinrishici-sentence">MelodyHub</span></p>
          <script src="https://sdk.jinrishici.com/v2/browser/jinrishici.js" charset="utf-8"></script>
        
      </div>
    
    
      <div class="social-wrapper">
        
          
            <a href="/blog/atom.xml"
              class="social fas fa-rss flat-btn"
              target="_blank"
              rel="external nofollow noopener noreferrer">
            </a>
          
        
          
            <a href="mailto:melodyjerry@163.com"
              class="social fas fa-envelope flat-btn"
              target="_blank"
              rel="external nofollow noopener noreferrer">
            </a>
          
        
          
            <a href="https://github.com/melodyjerry/melodyjerry.github.io"
              class="social fab fa-github flat-btn"
              target="_blank"
              rel="external nofollow noopener noreferrer">
            </a>
          
        
          
            <a href="https://music.163.com/#/user/home?id=247430004"
              class="social fas fa-headphones-alt flat-btn"
              target="_blank"
              rel="external nofollow noopener noreferrer">
            </a>
          
        
      </div>
    
  </div>
</section>

            
          
        
          
          
        
          
          
        
          
          
        
          
          
        
          
          
        
          
          
        
          
          
        
      
        
          
          
        
          
          
            
              <section class='widget plain'>
  
<header class='pure'>
  <div><i class="fas fa-file fa-fw" aria-hidden="true"></i>&nbsp;&nbsp;文本部件</div>
  
</header>

  <div class='content pure'>
    <p>这是一段支持markdown的文本<br><img src="https://img.vim-cn.com/4e/3c87a2fd28fd0b9c2f27ce3f83f1e9275d0488.png" alt=""></p>

  </div>
</section>

            
          
        
          
          
        
          
          
        
          
          
        
          
          
        
          
          
        
          
          
        
      
        
          
          
        
          
          
        
          
          
            
              
  <section class='widget toc-wrapper'>
    
<header class='pure'>
  <div><i class="fas fa-list fa-fw" aria-hidden="true"></i>&nbsp;&nbsp;本文目录</div>
  
    <!-- <div class='wrapper'><a class="s-toc rightBtn" rel="external nofollow noopener noreferrer" href="javascript:void(0)"><i class="fas fa-thumbtack fa-fw"></i></a></div> -->
  
</header>

    <div class='content pure'>
      <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#Java笔记——对象、类"><span class="toc-text">Java笔记——对象、类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#注脚："><span class="toc-text">注脚：</span></a></li></ol>
    </div>
  </section>


            
          
        
          
          
        
          
          
        
          
          
        
          
          
        
          
          
        
      
        
          
          
        
          
          
        
          
          
        
          
          
            
              <section class='widget grid'>
  
<header class='pure'>
  <div><i class="fas fa-map-signs fa-fw" aria-hidden="true"></i>&nbsp;&nbsp;站内导航</div>
  
</header>

  <div class='content pure'>
    <ul class="grid navgation">
      
        <li><a class="flat-box" title="/blog/" href="/blog/"
          
          
          id="blog">
          
            <i class="fas fa-clock fa-fw" aria-hidden="true"></i>
          
          近期文章
        </a></li>
      
        <li><a class="flat-box" title="/blog/blog/archives/" href="/blog/blog/archives/"
          
            rel="nofollow"
          
          
          id="blogblogarchives">
          
            <i class="fas fa-archive fa-fw" aria-hidden="true"></i>
          
          文章归档
        </a></li>
      
        <li><a class="flat-box" title="/blog/projects/" href="/blog/projects/"
          
          
          id="blogprojects">
          
            <i class="fas fa-code-branch fa-fw" aria-hidden="true"></i>
          
          开源项目
        </a></li>
      
        <li><a class="flat-box" title="/blog/friends/" href="/blog/friends/"
          
            rel="nofollow"
          
          
          id="blogfriends">
          
            <i class="fas fa-link fa-fw" aria-hidden="true"></i>
          
          我的友链
        </a></li>
      
        <li><a class="flat-box" title="https://xaoxuu.com/wiki/material-x/" href="https://xaoxuu.com/wiki/material-x/"
          
            rel="nofollow"
          
          
          id="https:xaoxuu.comwikimaterial-x">
          
            <i class="fas fa-book fa-fw" aria-hidden="true"></i>
          
          主题文档
        </a></li>
      
        <li><a class="flat-box" title="/blog/about/" href="/blog/about/"
          
            rel="nofollow"
          
          
          id="blogabout">
          
            <i class="fas fa-info-circle fa-fw" aria-hidden="true"></i>
          
          关于小站
        </a></li>
      
    </ul>
  </div>
</section>

            
          
        
          
          
        
          
          
        
          
          
        
          
          
        
      
        
          
          
        
          
          
        
          
          
        
          
          
        
          
          
            
              
  <section class='widget category'>
    
<header class='pure'>
  <div><i class="fas fa-folder-open fa-fw" aria-hidden="true"></i>&nbsp;&nbsp;文章分类</div>
  
    <a class="rightBtn"
    
      rel="nofollow"
    
    
    href="/blog/blog/categories/"
    title="blog/categories/">
    <i class="fas fa-expand-arrows-alt fa-fw"></i></a>
  
</header>

    <div class='content pure'>
      <ul class="entry">
        
          <li><a class="flat-box" title="/blog/categories/hexo/" href="/blog/categories/hexo/"><div class='name'>hexo</div><div class='badge'>(1)</div></a></li>
        
      </ul>
    </div>
  </section>


            
          
        
          
          
        
          
          
        
          
          
        
      
        
          
          
        
          
          
        
          
          
        
          
          
        
          
          
        
          
          
            
              
  <section class='widget tagcloud'>
    
<header class='pure'>
  <div><i class="fas fa-tags fa-fw" aria-hidden="true"></i>&nbsp;&nbsp;热门标签</div>
  
    <a class="rightBtn"
    
      rel="nofollow"
    
    
    href="/blog/blog/tags/"
    title="blog/tags/">
    <i class="fas fa-expand-arrows-alt fa-fw"></i></a>
  
</header>

    <div class='content pure'>
      <a href="/blog/tags/GitHub/" style="font-size: 14px; color: #999">GitHub</a> <a href="/blog/tags/Hexo/" style="font-size: 18px; color: #7e7e7e">Hexo</a> <a href="/blog/tags/Java/" style="font-size: 24px; color: #555">Java</a> <a href="/blog/tags/SQL-Server/" style="font-size: 16px; color: #8b8b8b">SQL Server</a> <a href="/blog/tags/%E5%8D%9A%E5%AE%A2/" style="font-size: 16px; color: #8b8b8b">博客</a> <a href="/blog/tags/%E5%B7%A5%E5%85%B7/" style="font-size: 16px; color: #8b8b8b">工具</a> <a href="/blog/tags/%E7%AC%94%E8%AE%B0/" style="font-size: 22px; color: #636363">笔记</a> <a href="/blog/tags/%E8%84%9A%E6%9C%AC/" style="font-size: 14px; color: #999">脚本</a> <a href="/blog/tags/%E9%9A%8F%E7%AC%94/" style="font-size: 20px; color: #707070">随笔</a> <a href="/blog/tags/%E9%9F%B3%E4%B9%90/" style="font-size: 14px; color: #999">音乐</a>
    </div>
  </section>


            
          
        
          
          
        
          
          
        
      
        
          
          
        
          
          
        
          
          
        
          
          
        
          
          
        
          
          
        
          
          
        
          
          
            
              <section class='widget list'>
  
<header class='pure'>
  <div><i class="fas fa-thumbs-up fa-fw" aria-hidden="true"></i>&nbsp;&nbsp;强烈推荐</div>
  
</header>

  <div class='content pure'>
    <ul class="entry">
      
        <li><a class="flat-box" title="https://xaoxuu.com/wiki/hexo.sh/" href="https://xaoxuu.com/wiki/hexo.sh/"
          
          
          >
          <div class='name'>
            
              <i class=" fa-fw" aria-hidden="true"></i>
            
            &nbsp;&nbsp;Hexo脚本（Mac）
          </div>
          
        </a></li>
      
        <li><a class="flat-box" title="https://xaoxuu.com/wiki/vim-cn.sh/" href="https://xaoxuu.com/wiki/vim-cn.sh/"
          
          
          >
          <div class='name'>
            
              <i class=" fa-fw" aria-hidden="true"></i>
            
            &nbsp;&nbsp;图床脚本（Mac）
          </div>
          
        </a></li>
      
        <li><a class="flat-box" title="https://yasuotu.com" href="https://yasuotu.com"
          
          
          >
          <div class='name'>
            
              <i class=" fa-fw" aria-hidden="true"></i>
            
            &nbsp;&nbsp;图片在线压缩
          </div>
          
        </a></li>
      
        <li><a class="flat-box" title="https://realfavicongenerator.net" href="https://realfavicongenerator.net"
          
          
          >
          <div class='name'>
            
              <i class=" fa-fw" aria-hidden="true"></i>
            
            &nbsp;&nbsp;生成Favicon
          </div>
          
        </a></li>
      
        <li><a class="flat-box" title="https://mxclub.github.io/resume/" href="https://mxclub.github.io/resume/"
          
          
          >
          <div class='name'>
            
              <i class=" fa-fw" aria-hidden="true"></i>
            
            &nbsp;&nbsp;简历主题
          </div>
          
        </a></li>
      
    </ul>
  </div>
</section>

            
          
        
      
        
          
          
        
          
          
        
          
          
        
          
          
        
          
          
        
          
          
        
          
          
        
          
          
        
      
    

  
</aside>

<footer id="footer" class="clearfix">
  
  
    <div class="social-wrapper">
      
        
          <a href="/blog/atom.xml"
            class="social fas fa-rss flat-btn"
            target="_blank"
            rel="external nofollow noopener noreferrer">
          </a>
        
      
        
          <a href="mailto:melodyjerry@163.com"
            class="social fas fa-envelope flat-btn"
            target="_blank"
            rel="external nofollow noopener noreferrer">
          </a>
        
      
        
          <a href="https://github.com/melodyjerry/melodyjerry.github.io"
            class="social fab fa-github flat-btn"
            target="_blank"
            rel="external nofollow noopener noreferrer">
          </a>
        
      
        
          <a href="https://music.163.com/#/user/home?id=247430004"
            class="social fas fa-headphones-alt flat-btn"
            target="_blank"
            rel="external nofollow noopener noreferrer">
          </a>
        
      
    </div>
  
  <br>
  <div><p>博客内容遵循 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank" rel="noopener">署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议</a></p>
</div>
  <div>
    本站使用
    <a href="https://xaoxuu.com/wiki/material-x/" target="_blank" class="codename">Material X</a>
    作为主题
    
      ，
      总访问量为
      <span id="busuanzi_value_site_pv"><i class="fas fa-spinner fa-spin fa-fw" aria-hidden="true"></i></span>
      次
    
    。
  </div>
</footer>
<script>setLoadingBarProgress(80);</script>


      <script>setLoadingBarProgress(60);</script>
    </div>
    <a class="s-top fas fa-arrow-up fa-fw" href='javascript:void(0)'></a>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>

  <script>
    var GOOGLE_CUSTOM_SEARCH_API_KEY = "";
    var GOOGLE_CUSTOM_SEARCH_ENGINE_ID = "";
    var ALGOLIA_API_KEY = "";
    var ALGOLIA_APP_ID = "";
    var ALGOLIA_INDEX_NAME = "";
    var AZURE_SERVICE_NAME = "";
    var AZURE_INDEX_NAME = "";
    var AZURE_QUERY_KEY = "";
    var BAIDU_API_ID = "";
    var SEARCH_SERVICE = "hexo" || "hexo";
    var ROOT = "/blog/"||"/";
    if(!ROOT.endsWith('/'))ROOT += '/';
  </script>

<script src="//instant.page/1.2.2" type="module" integrity="sha384-2xV8M5griQmzyiY3CDqh1dn4z3llDVqZDqzjzcY+jCBCk/a5fXJmuZ/40JJAPeoU"></script>


  <script async src="https://cdn.jsdelivr.net/npm/scrollreveal@4.0.5/dist/scrollreveal.min.js"></script>
  <script type="text/javascript">
    $(function() {
      const $reveal = $('.reveal');
      if ($reveal.length === 0) return;
      const sr = ScrollReveal({ distance: 0 });
      sr.reveal('.reveal');
    });
  </script>


  <script src="https://cdn.jsdelivr.net/npm/node-waves@0.7.6/dist/waves.min.js"></script>
  <script type="text/javascript">
    $(function() {
      Waves.attach('.flat-btn', ['waves-button']);
      Waves.attach('.float-btn', ['waves-button', 'waves-float']);
      Waves.attach('.float-btn-light', ['waves-button', 'waves-float', 'waves-light']);
      Waves.attach('.flat-box', ['waves-block']);
      Waves.attach('.float-box', ['waves-block', 'waves-float']);
      Waves.attach('.waves-image');
      Waves.init();
    });
  </script>


  <script async src="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-busuanzi@2.3/js/busuanzi.pure.mini.js"></script>




  
  
  
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-backstretch/2.0.4/jquery.backstretch.min.js"></script>
    <script type="text/javascript">
      $(function(){
        if ('.cover') {
          $('.cover').backstretch(
          ["https://img.vim-cn.com/29/91197b04c13f512f734a76d4ac422d89dbe229.jpg"],
          {
            duration: "6000",
            fade: "2500"
          });
        } else {
          $.backstretch(
          ["https://img.vim-cn.com/29/91197b04c13f512f734a76d4ac422d89dbe229.jpg"],
          {
            duration: "6000",
            fade: "2500"
          });
        }
      });
    </script>
  









  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  
    
      
<script src="/blog/js/volantis.js"></script>

    
  
  <script>
  var GUEST_INFO = ['nick','mail','link'];
  var guest_info = 'nick,mail,link'.split(',').filter(function(item){
    return GUEST_INFO.indexOf(item) > -1
  });
  var notify = 'true' == true;
  var verify = 'true' == true;
  var valine = new Valine();
  valine.init({
    el: '#valine_container',
    notify: notify,
    verify: verify,
    guest_info: guest_info,
    
    appId: "UNKL0FXJFAFOKRM1mGyL3Y2s-gzGzoHsz",
    appKey: "HjUUcTY42RErTnIt7O58GhvH",
    placeholder: "( ͡° ͜ʖ ͡°)✧ 来啊~",
    pageSize:'10',
    avatar:'mp',
    lang:'zh-cn',
    highlight:'true'
  })
  </script>



  
<script src="/blog/js/app.js"></script>



  
<script src="/blog/js/search.js"></script>





<!-- 复制 -->
<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  let COPY_SUCCESS = "复制成功";
  let COPY_FAILURE = "复制失败";
  /*页面载入完成后，创建复制按钮*/
  !function (e, t, a) {
    /* code */
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '  <i class="fa fa-copy"></i><span>复制</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });

      clipboard.on('success', function(e) {
        //您可以加入成功提示
        console.info('Action:', e.action);
        console.info('Text:', e.text);
        console.info('Trigger:', e.trigger);
        success_prompt(COPY_SUCCESS);
        e.clearSelection();
      });
      clipboard.on('error', function(e) {
        //您可以加入失败提示
        console.error('Action:', e.action);
        console.error('Trigger:', e.trigger);
        fail_prompt(COPY_FAILURE);
      });
    }
    initCopyCode();

  }(window, document);

  /**
   * 弹出式提示框，默认1.5秒自动消失
   * @param message 提示信息
   * @param style 提示样式，有alert-success、alert-danger、alert-warning、alert-info
   * @param time 消失时间
   */
  var prompt = function (message, style, time)
  {
      style = (style === undefined) ? 'alert-success' : style;
      time = (time === undefined) ? 1500 : time*1000;
      $('<div>')
          .appendTo('body')
          .addClass('alert ' + style)
          .html(message)
          .show()
          .delay(time)
          .fadeOut();
  };

  // 成功提示
  var success_prompt = function(message, time)
  {
      prompt(message, 'alert-success', time);
  };

  // 失败提示
  var fail_prompt = function(message, time)
  {
      prompt(message, 'alert-danger', time);
  };

  // 提醒
  var warning_prompt = function(message, time)
  {
      prompt(message, 'alert-warning', time);
  };

  // 信息提示
  var info_prompt = function(message, time)
  {
      prompt(message, 'alert-info', time);
  };

</script>


<!-- fancybox -->
<script src="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>
<script>
  let LAZY_LOAD_IMAGE = "";
  $(".article-entry").find("fancybox").find("img").each(function () {
      var element = document.createElement("a");
      $(element).attr("data-fancybox", "gallery");
      $(element).attr("href", $(this).attr("src"));
      /* 图片采用懒加载处理时,
       * 一般图片标签内会有个属性名来存放图片的真实地址，比如 data-original,
       * 那么此处将原本的属性名src替换为对应属性名data-original,
       * 修改如下
       */
       if (LAZY_LOAD_IMAGE) {
         $(element).attr("href", $(this).attr("data-original"));
       }
      $(this).wrap(element);
  });
</script>





  <script>setLoadingBarProgress(100);</script>
</body>
</html>
